<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'模板列表'),
	array('op'=>'edit','title'=>'添加主题'),	
);
$return['allgames2']=$this->allgames2;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$gamename=$this->gamename;
	$params['gamename']=$gamename;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['ptitle'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['ptitle'])){
		$pids=pdo_getall('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'title like %'=>$_RQ['ptitle'].'%'),'id','id');
		$params['pid']=array_keys($pids);
		$return['params']['ptitle']=$_RQ['ptitle'];
	}
	if(!empty($_RQ['merchname'])){
		$mid=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'merchname like %'=>$_RQ['merchname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	if(isset($_RQ['status'])) {
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=intval($_RQ['status']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_merch_game_item_tpl',$params,array($pindex,$psize),$total,$fields,'','paixu desc,id desc');
	if($list){
		foreach($list as &$item){
			$pitem=pdo_get('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pid']));
			$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$pitem['mid']));
			$item['ptitle']=$pitem['title'];
			$item['merchname']=$merch['源自活动'];
			$item['merchlogo']=$merch['logo'];
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'可以选择某活动主题，直接生成模板！！','edit'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'logo','title'=>'模板LOGO','type'=>'image'),
			array('name'=>'title','title'=>'模板名称','type'=>'string'),
			array('name'=>'ptitle','title'=>'源自活动','type'=>'string'),
			array('name'=>'merchname','title'=>'源自商家','type'=>'string'),
			array('name'=>'gamename','title'=>'类型','type'=>'radio','options'=>'allgames2'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'title','width'=>15,'title'=>'模板名称','type'=>'string'),
		)
	);
	$return['status']=$this->tpl_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$gamename=$this->gamename;
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_merch_game_item_tpl',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['goods']=json_decode($item['goods'],true);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回模板列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'模板名称','type'=>'string'),
					array('name'=>'logo','title'=>'模板描述','type'=>'image'),
					array('name'=>'desc','title'=>'模板描述','type'=>'string'),
					array('name'=>'groupid','title'=>'分类','type'=>'select','options'=>'cate','option_title'=>'catename'),
					array('name'=>'content','title'=>'模板内容','type'=>'richtext'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
				)
			),
			array(
				'title'=>'商品内容',
					'list'=>array(
						array(
							'type'=>'array',
							'name'=>'goods',
							'add_tip'=>'添加商品',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'商品名称','help'=>''),
									array('type'=>'image','name'=>'image','title'=>'商品图片','help'=>''),
									array('type'=>'unitstring','unit'=>'元','name'=>'price','title'=>'商品价格','help'=>''),
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'商品名称','help'=>''),
									array('type'=>'image','name'=>'image','title'=>'商品图片','help'=>''),
									array('type'=>'unitstring','unit'=>'元','name'=>'price','title'=>'商品价格','help'=>''),
								)
						),
					)
			),
		)
	);
	$return['types']=array(1=>'文字',3=>'单图',10=>'多图',11=>'视频',100=>'富文本');
	$return['cate']=pdo_getall('jy_merch_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$id=intval($postdata['id']);
	if(!$postdata['title']){
		exi('请填写模板名称！','error');
	}
	if(!$postdata['content']){
		exi('请填写模板内容！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'logo'=>trim($postdata['logo']),
		'content'=>trim($postdata['content']),
		'groupid'=>intval($postdata['groupid']),
		'paixu'=>intval($postdata['paixu']),
		'status'=>intval($postdata['status']),
		'price'=>trim($postdata['price']),
		'goods'=>ijson_encode($postdata['goods']),
	);
	if($id){
		pdo_update('jy_merch_game_item_tpl',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['gamename']=$this->gamename;
		$data['createtime']=time();
		pdo_insert('jy_merch_game_item_tpl',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_merch_game_item_tpl',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
